﻿/*
错误消息控制类
*/
if (!va)
{
    var va = {};
}
if (!va.ui)
{
    va.ui = {};
}


//va.ui
(function() {
    //abstract: 错误类别基类
    function errbase() {
        this.showerred = false; //是否已经显示错误信息
    }
    errbase.prototype.errmsg = "";


    //---------------------------------------
    //public : 附加错误消息
    function createerr() { }
    createerr.prototype = new errbase();

    //清除错误消息
    createerr.prototype.clearerr = function() {
        var haserred = false;
        var errobj;
        if (!(obj && obj.nodeType == 1)) {

            return;

        }

        //附加错误消息
        //处理复选框
        var Obj = obj;
        var cnode;
        if ((Obj.type && Obj.type == "checkbox") || (Obj.type && Obj.type == "radio")) {
            if (Obj.name && document.getElementsByName(Obj.name).length > 0) {
                Obj = document.getElementsByName(Obj.name)[document.getElementsByName(Obj.name).length - 1];

                if (Obj.nextSibling.nodeType == 3) {



                    if (Obj.nextSibling.nextSibling.tagName == "LABEL" && Obj.nextSibling.nextSibling.getAttribute("haserred") == null) {
                        alert("移除错误");
                        haserred = true;
                        errobj = Obj.nextSibling.nextSibling;
                    }

                    cnode = Obj.nextSibling;
                }
                else {
                    if (Obj.nextSibling.tagName == "LABEL" && Obj.nextSibling.getAttribute("haserred") != null) {
                        alert("移除错误错误");
                        haserred = true;
                        errobj = Obj.nextSibling;
                    }
                    cnode = Obj;
                }

            }
        }
        else {
            if (Obj.nextSibling.tagName == "LABEL" && Obj.nextSibling.getAttribute("haserred") != null) {
                alert("移除错误错误");
                haserred = true;
                errobj = Obj.nextSibling;
            }
            cnode = Obj;
        }
    };

    //@param {object} obj 
    createerr.prototype.show = function(obj) {

        var haserred = false;
        var errobj;
        if (!(obj && obj.nodeType == 1)) {

            return;
        }


        //在当前节点后插入新节点
        //@param {object} n 被插入的节点
        //@r {object} r 当前节点
        var insertAfter = function(n, r) {
            if (r.nextSibling)
                r.parentNode.insertBefore(n, r.nextSibling);
            else
                r.parentNode.appendChild(n);
        };

        //附加错误消息
        //处理复选框
        var Obj = obj;
        var cnode;
        if ((Obj.type && Obj.type == "checkbox") || (Obj.type && Obj.type == "radio")) {
            if (Obj.name && document.getElementsByName(Obj.name).length > 0) {
                Obj = document.getElementsByName(Obj.name)[document.getElementsByName(Obj.name).length - 1];

                if (Obj.nextSibling.nodeType == 3) {

                    //                if (this.haserr(Obj))
                    //                {
                    //                    
                    //                    Obj.nextSibling.nextSibling.innerHTML  = this.errmsg;
                    //                    return;
                    //                }

                    if (Obj.nextSibling.nextSibling.tagName == "LABEL" && Obj.nextSibling.nextSibling.getAttribute("haserred") == null) {
                        //alert("已经错误");
                        haserred = true;
                        errobj = Obj.nextSibling.nextSibling;
                    }

                    cnode = Obj.nextSibling;
                }
                else {
                    if (Obj.nextSibling.tagName == "LABEL" && Obj.nextSibling.getAttribute("haserred") != null) {
                        //alert("已经错误");
                        haserred = true;
                        errobj = Obj.nextSibling;
                    }
                    cnode = Obj;
                }

            }
        }
        else {
            if (Obj.nextSibling.tagName == "LABEL" && Obj.nextSibling.getAttribute("haserred") != null) {
                //alert("已经错误");
                haserred = true;
                errobj = Obj.nextSibling;
            }
            cnode = Obj;
        }

        if (haserred) {
            errobj.innerHTML = this.errmsg;

        }
        else {
            var newlabel = document.createElement("label");
            newlabel.setAttribute("haserred", "true"); //设置错误属性
            newlabel.className = "err"; //label.err设置css
            newlabel.innerHTML = this.errmsg;
            insertAfter(newlabel, cnode);
        }
    };

    //public:将错误消息显示在指定的容器内
    va.ui.createerr = createerr;


    //-------------------------------
    //在指定容器中显示错误消息
    //@param {string} id 错误容器的id
    function showmsg() {
        this.errid = "";
    }
    showmsg.prototype = new errbase();
    //是否已经显示错误
    showmsg.prototype.haserr = function() {
        if (this.errid != "" && document.getElementById(this.errid).innerHTML != "") {
            return true;
        }
    };

    //清除错误
    showmsg.prototype.clearerr = function() { };

    showmsg.prototype.show = function(id) {

        if (!(typeof id == "string") && !(typeof this.errmsg == "string")) {
            return;
        }
        try {
            this.errid = id;
            document.getElementById(id).innerHTML = this.errmsg;
        }
        catch (e) {
            alert("未找到对象");
        }

    };

    //public
    va.ui.showmsg = showmsg;


    //-------------------------------------
    //private: 弹出错误消息框
    //@param {string} errmsg 错误消息
    function alert_err() { }
    alert_err.prototype = new errbase();
    alert_err.prototype.show = function(obj) {
        if (!(typeof this.errmsg == "string")) {
            return;
        }

        //财神设置错误
        //plutus.seterror(obj);


        alert(this.errmsg);


        obj.focus();

        throw new Object(); //抛出异常终止程序执行
    };

    //public
    va.ui.alert_err = alert_err;


    //错误工厂类
    va.ui.errfac = function(mode, msg, obj) {

        switch (mode) {
            case "alert":
                //alert
                var alerterr = new va.ui.alert_err();
                alerterr.errmsg = msg;
                alerterr.show(obj);
                //end
                break;

            case "append":
                //附加错误消息
                var temp1 = new va.ui.createerr();
                temp1.errmsg = msg;

                //@param {object} 需要附加的对象
                temp1.show(obj);
                //end
                break;

            case "errcontent":
                //指定容器
                var errcontent = new va.ui.showmsg();
                errcontent.errmsg = msg;
                //@param {string} 容器的id
                errcontent.show(obj);
                break;
        }
    };





})();

